local skel = fk.CreateSkill {
  name = "efengqi__jieliang",
}

Fk:loadTranslationTable{
  ["efengqi__jieliang"] = "截粮",
  [":efengqi__jieliang"] = "每轮限一次，其他角色摸牌阶段开始时，你可以弃置一张黑色非锦囊牌，观看并使用牌堆顶两张牌，每使用一张牌，此阶段少摸一张牌。",

  ["#efengqi__jieliang-invoke"] = "截粮：你可以弃置一张黑色非锦囊牌，观看并使用牌堆顶两张牌，每使用一张令 %dest 少摸一张牌",
  ["#efengqi__jieliang-use"] = "截粮：你可以使用其中一张牌",

  ["$efengqi__jieliang1"] = "截其源，断其粮，贼可擒也。",
  ["$efengqi__jieliang2"] = "人是铁，饭是钢。",
}

skel:addEffect(fk.EventPhaseStart, {
  anim_type = "control",
  times = function (_, player)
    return 1 - player:usedSkillTimes(skel.name, Player.HistoryRound)
  end,
  can_trigger = function(self, event, target, player, data)
    return target ~= player and player:hasSkill(skel.name) and target.phase == Player.Draw and not player:isNude() and
    not data.phase_end and not target.dead and
    player:usedSkillTimes(skel.name, Player.HistoryRound) == 0
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToDiscard(player, {
      min_num = 1, max_num = 1, skill_name = skel.name, cancelable = true, skip = true,
      pattern = ".|.|spade,club|.|.|basic,equip", include_equip = true,
      prompt = "#efengqi__jieliang-invoke::"..target.id,
    })
    if #cards > 0 then
      event:setCostData(self, { tos = {target}, cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:throwCard(event:getCostData(self).cards, skel.name, player, player)
    if player.dead then return end
    local cards = room:getNCards(2)
    while not player.dead and #cards > 0 do
      local use = room:askToUseRealCard(player, {
        pattern = cards, skill_name = skel.name, prompt = "#efengqi__jieliang-use", skip = false,
        expand_pile = cards, extra_data = {
          bypass_times = true,
          extraUse = true,
          expand_pile = cards,
        }
      })
      if use then
        cards = table.filter(cards, function(id) return room:getCardArea(id) == Card.DrawPile end)
        table.removeOne(cards, use.card.id)
        room:addPlayerMark(target, "efengqi__jieliang-phase", 1)
      else
        break
      end
    end
  end,
})

skel:addEffect(fk.DrawNCards, {
  can_refresh = function (self, event, target, player, data)
    return target == player and player:getMark("efengqi__jieliang-phase") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.n = data.n - player:getMark("efengqi__jieliang-phase")
  end,
})


return skel
